//
//  PurchasingMemberVC.m
//  SelfDriving
//
//  Created by apple on 16/1/19.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "PurchasingMemberVC.h"
#import "BlackCell.h"
#import "BusinessService+WD.h"
#import <AlipaySDK/AlipaySDK.h>

#import "BusinessService+User.h"

#import "WXApi.h"
#import "WXApiManager.h"
#import "WXApiObject.h"
@interface PurchasingMemberVC ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,WXApiManagerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *nexitBtn;
@property (weak, nonatomic) IBOutlet UITableView *table;

@end

@implementation PurchasingMemberVC
{
    NSMutableArray *data;
    int index;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect cg = _table.frame;
    cg.origin.y = 64;
    cg.size.height =SCRE_HEIGHT-( 64+_nexitBtn.frame.size.height);
    _table.frame = cg;
    
    [[BusinessService shareInstance]member_fee:[[NSMutableDictionary alloc]init] success:^(id result) {
        data = result;
        [_table reloadData];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
    [WXApiManager sharedManager].delegate = self;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return data.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BlackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VipCell"];
    LoginResponseModel *model = [data objectAtIndex:indexPath.row];
    cell.labelname.text = model.title;
    [cell.monerBtn setTitle:[NSString stringWithFormat:@"%@元",model.fee] forState:UIControlStateNormal];
    CGSize s = [ValidateHelper sizeOfText:[NSString stringWithFormat:@"%@元",model.fee] theFont:[UIFont systemFontOfSize:12]];
    CGRect cg =  cell.monerBtn.frame;
    cg.size.width = s.width+15;
    cell.monerBtn.frame = cg;
    if (index == indexPath.row) {
        cell.btnClater.selected = YES;
    }else{
        cell.btnClater.selected = NO;
    }
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    index = (int)indexPath.row;
    [_table reloadData];
}
#pragma  mark 支付
- (IBAction)submitAction:(UIButton *)sender {
//    UIActionSheet *alrt = [[UIActionSheet alloc]initWithTitle:@"选择充值方式" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"支付宝支付",@"微信支付",@"余额支付", nil];
    
    UIActionSheet *alrt = [[UIActionSheet alloc]initWithTitle:@"选择充值方式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"支付宝支付",@"微信支付",@"余额支付", nil];
    [alrt showInView:self.view];
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    LoginResponseModel *user = [AppDelegate shareDelegate].user;
    LoginResponseModel *model = [data objectAtIndex:index];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:model.id forKey:@"memberid"];
    if ([user.memberlevel intValue] == 0) {
        [dic setObject:@"1" forKey:@"chongzhi_type"];
    }
    else {
        [dic setObject:@"2" forKey:@"chongzhi_type"];
    }
    
    if (buttonIndex == 0) {
        [dic setObject:@"0" forKey:@"action"];
    }
    if (buttonIndex == 1) {
        [dic setObject:@"1" forKey:@"action"];
    }
    if (buttonIndex == 2) {
        [dic setObject:@"2" forKey:@"action"];
    }
    
    [dic setObject:model.fee forKey:@"fee"];
    [[BusinessService shareInstance]recharege:dic success:^(id result) {
        LoginResponseModel *user = result;
        if (buttonIndex == 0) {
            NSString *responseString = [[NSString stringWithFormat:@"%@",user.data] stringByReplacingOccurrencesOfString:@"\"" withString:@"\""];
            //之前的fromScheme参数传的是SelfDriving并且在info.plist中没有添加对应的scheme;现在改为yimi并在info.plist中添加对应的scheme(xhw11月14号改)
            [[AlipaySDK defaultService] payOrder:responseString fromScheme:@"yimi" callback:^(NSDictionary *resultDic) {
                if([[resultDic objectForKey:@"resultStatus"] intValue]==9000){
                    [SVProgressHUD showSuccessWithStatus:@"充值成功"];
                }
                else {
                    [SVProgressHUD showSuccessWithStatus:@"充值失败"];
                }
                
                [[self navigationController] popViewControllerAnimated:YES];
            }];
        }
        if (buttonIndex == 1) {
            
//            BOOL isSuccess = [WXApi registerApp:user.appid withDescription:@"com.xizue.yimi"];
            
            BOOL isSuccess = [WXApi registerApp:user.appid];
            
            //调起微信支付
            PayReq* req             = [[PayReq alloc] init];
            req.openID              = user.appid;
            req.partnerId           = user.partnerid;
            req.prepayId            = user.prepayid;
            req.nonceStr            = user.nonceStr;
            req.timeStamp           = [user.timestamp intValue];
            req.package             = user.package;
            req.sign                = user.sign;
            [WXApi sendReq:req];
        }
        if (buttonIndex == 2) {
            [[AppDelegate shareDelegate]refdata:^(id as) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
}
- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[PayResp class]]) {
        PayResp *response = (PayResp *)resp;
        switch (response.errCode) {
            case WXSuccess:
                [SVProgressHUD showSuccessWithStatus:@"充值成功"];
                break;
                
            default:
                NSLog(@"支付失败,retcode=%d",response.errCode);
                break;
        }
    }
}
- (void)responseFromWXChatPay:(PayResp *)resp {
    switch (resp.errCode) {
        case WXSuccess:
            NSLog(@"支付成功");
            break;
            
        default:
            NSLog(@"支付失败,retcode=%d",resp.errCode);
            break;
    }
    [[self navigationController] popViewControllerAnimated:YES];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
